package com.stefanini.worldcupinformation.controladores.team;

import com.stefanini.worldcupinformation.controladores.EditGroupController;
import com.stefanini.worldcupinformation.ejbs.GroupDao;
import com.stefanini.worldcupinformation.ejbs.TeamDao;
import com.stefanini.worldcupinformationpersistence.entities.Equipo;
import com.stefanini.worldcupinformationpersistence.entities.Grupo;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("editTeamController")
@SessionScoped
public class EditTeamController implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @EJB
    private TeamDao teamDao;
    
    @EJB
    private GroupDao groupDao;
    
    private Equipo team;
    private List<Grupo> groups;
    
    public void save(){
        try {
            this.teamDao.edit(team);
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/team/teamAdmin.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EditTeamController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void delete(Equipo team){
        try {
            teamDao.delte(team);
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/team/teamAdmin.xhtml");
        } catch (Exception e) {
            Logger.getLogger(EditGroupController.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    
    public void initEdit(Equipo team){
        try {
            this.team = team;
            this.initGroups();
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/team/editTeam.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EditTeamController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Equipo getTeam() {
        return team;
    }

    public void setTeam(Equipo team) {
        this.team = team;
    }
    
    public List<Grupo> getGroups() {
        return groups;
    }

    public void setGroups(List<Grupo> groups) {
        this.groups = groups;
    }
    
    private void initGroups(){
        this.groups = this.groupDao.getAll();
    }
}
